﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FDSimpleModelGenerator.Common
{
    /// <summary>
    /// 枚举工具
    /// </summary>
    public class EnumUtils
    {
        /// <summary>
        /// 获取枚举类型项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <returns></returns>
        public static IList<EnumItem<V>> GetEnumList<T, V>() where T : struct
        {
            var names = Enum.GetNames(typeof(T));
            var list = new List<EnumItem<V>>();
            foreach (var name in names)
            {
                var enumItem = Enum.Parse<T>(name);
                var da = enumItem.GetType().GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
                var desc = da?.Description ?? string.Empty;
                list.Add(new EnumItem<V>
                {
                    Name = name,
                    Text = desc,
                    Value = (V)Convert.ChangeType(enumItem, typeof(V))
                });
            }

            return list;
        }
    }

    /// <summary>
    /// 枚举项
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class EnumItem<T>
    {
        /// <summary>
        /// 枚举名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 枚举描述
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 枚举值
        /// </summary>
        public T Value { get; set; }
    }
}
